<?php
    require_once("include/game_core.php");
    require_once(DB_FILE);
    require_once(USER_CONTROLLER_FILE);
    require_once(GAME_INIT_FILE);
    
    // first access this page only if logged it
    if ( ! UserController::isLoggedIn() ) {
        die();
    }    

    // seond access this page only after visited in waiting room
    if ( UserController::isWatingForGame() ) {  //set by the WaitingRoom in the first time
        if ( isset($_SESSION["waiting_started"]) &&  $_SESSION["waiting_started"] ) {    //if this is NOT the first time we are in "waiting_state"
            // Already Wait (AJAX)
            // check for updates
          
            $result = GameInit::CheckForUpdate();
            
            if ( $result ) {
                // OK
                // OK_START_GAME / WAIT_TO_START_GAME / 
                echo $result;
            }
            else {
                //echo 'NOT OK GameInit::CheckForUpdate()' . '<br>';
                if (DEBUG) {
                    echo ERROR_WAITING . 'CheckForUpdate' . GameInit::getErrorMSG();
                }
                else {
                    echo ERROR_WAITING;
                }
            }
        }
        else {
            // first time to wait
            $result = GameInit::StartWaiting();
            if ( $result ) {
                // Ok
                // OK_START_GAME / WAIT_TO_START_GAME / 
                echo $result;
            }
            else {
                //echo 'NOT OK GameInit::StartWaiting()' . '<br>';
                if (DEBUG) {
                    echo ERROR_WAITING . '-StartWaiting:' .GameInit::getErrorMSG();
                }
                else {
                    echo ERROR_WAITING;
                }
            }
        }
    }
    else {  //isWatingForGame() == false ... 
        die();
    }